package com.sky.controller.user;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.result.Result;
import com.sky.service.IDishFlavorService;
import com.sky.service.IDishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.sky.constant.RedisConstants.DISH_KEY;
import static com.sky.constant.RedisConstants.DISH_KEY_TTL;

@RequestMapping("/user/dish")
@RestController("userDishController")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {

    @Autowired
    private IDishService dishService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/list")
    @ApiOperation("根据分类Id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        //构造redis中的key，规则 ：dish_分类id
        String key = DISH_KEY + categoryId;
        log.info("根据分类Id查询菜品{}", categoryId);

        //查询redis是否存在菜品数据
        String dishJson = stringRedisTemplate.opsForValue().get(key);
        List<DishVO> list = null;
        if (StrUtil.isNotBlank(dishJson)) {
            //如果存在，直接返回
            list = JSONUtil.toList(dishJson, DishVO.class);

            return Result.success(list);
        }

        //1.构造条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, StatusConstant.ENABLE);
        //2.得到了菜品列表
        List<Dish> dishList = dishService.list(wrapper);
        //3.封装菜品VO
        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish dish : dishList) {

            DishVO dishVO = new DishVO();
            BeanUtil.copyProperties(dish, dishVO);

//           根据菜品Id查询对应的口味
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> flavors = dishFlavorMapper.selectList(wrapper1);
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);

        }
        //不存在情况下，将菜品数据存入redis
        //将菜品数据存入redis
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(dishVOList), DISH_KEY_TTL, TimeUnit.MINUTES);
        return Result.success(dishVOList);


    }

}
